﻿/* $RCSFile: DownLoadGenerator.aspx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice.ECommerce/PageModules/Download/DownLoadGenerator.aspx.cs $
 * $log$
 * Revision 21 2010/08/09 11:08:11  christian.surieux
 *   Added Log comment to all .ascx .aspx .master files
 * Revision 8 2010/08/09 10:07:05  christian.surieux
 *   Added Log comment to all cs files
 */

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Globalization;

using AddonNice.UI;
using AddonNice.Settings;
using AddonNice.UI.WebControls;
using AddonNice.Security;
using AddonNice.Configuration;
using AddonNice.Diagnostics;
using AddonNice.Helpers;
using AddonNice.Authentication;
using AddonNice.ECommerce;
using AddonNice.Settings.Download;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;
using Path2     =   AddonNice.Settings.Path;


namespace AddonNice.PageModules.Download
{
    public partial class DownLoadGenerator : Page
    {
        Prod2DownLoadDB dwnDB   =   new Prod2DownLoadDB();

        protected void Page_Load(object sender, EventArgs e)
        {
			if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DownLoadGenerator Page_Load BEG Request.IsAuthenticated: {0}.",Request.IsAuthenticated),ModuleTraceSwitch.Sw.Info);
            if (IsPostBack || !Request.IsAuthenticated )
            {
                PortalSecurity.AccessDenied();
            }
            SqlDataReader dr            =   null;
            string VersionFileName      =   string.Empty;
            try
            {
                string versionIdStr     =   Request.Params["VFId"];
                string UserDownLoadStr  =   Request.Params["UDwnId"];
                int UserDownLoadId      =   int.Parse(UserDownLoadStr);
                int VersionFileId       =   int.Parse(versionIdStr);
			    if ( ModuleTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DownLoadGenerator Page_Load UserDownLoadId: {0}, VersionFileId: {1}",UserDownLoadId,VersionFileId),ModuleTraceSwitch.Sw.Info);
                dr                      =   dwnDB.GetVersionFile(VersionFileId);
                dr.Read();
                VersionFileName         =   (string)dr["VersionFileName"];
			    int ordinal             =   dr.GetOrdinal("BinProduct");
                byte[] buffer           =   dwnDB.GetFromSqlDataReaderByChunks(dr,ordinal);
			    if ( ModuleTraceSwitch.Sw.TraceVerbose )
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]DownLoadGenerator Page_Load BinProduct.Length: {0}",buffer.Length),ModuleTraceSwitch.Sw.Info);
                dr.Close();
                if ( UserDownLoadId != -1 )
                    dwnDB.IncUserDownLoadCount(UserDownLoadId);

			    Response.ClearHeaders();
			    Response.ClearContent();
			    Response.ContentType	=   "application/octet-stream";
			    Response.AddHeader("Content-Disposition", "attachment; filename=" + VersionFileName);
				Response.BinaryWrite(buffer);
			}
			catch(Exception ex)
			{
				if (ModuleTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]DownLoadGenerator Page_Load filename: {0}, ex: {1}",VersionFileName,ex),ModuleTraceSwitch.Sw.Info);
                throw;
			}
			finally
			{
				if ( dr != null )
					dr.Close();
			}
			if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]DownLoadGenerator Page_Load END.",ModuleTraceSwitch.Sw.Info);
			Response.End();
        }
    }
}
